package com.panda.takeaway.controller;

import com.panda.takeaway.auth.util.SecurityUtil;
import com.panda.takeaway.entity.bo.LoginBo;
import com.panda.takeaway.entity.bo.PwdBo;
import com.panda.takeaway.entity.bo.UserLoginBo;
import com.panda.takeaway.entity.dto.CommonResult;
import com.panda.takeaway.entity.po.Address;
import com.panda.takeaway.entity.po.User;
import com.panda.takeaway.entity.vo.UserInfoVo;
import com.panda.takeaway.service.OrderService;
import com.panda.takeaway.service.UserService;
import com.panda.takeaway.service.impl.OrderServiceImpl;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wjh
 * @date 2021/8/7 18:07
 * @Package com.panda.takeaway.controller
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    UserService userService;
    private OrderService orderService =new OrderServiceImpl();

    @RequestMapping("/login")
    public CommonResult<UserLoginBo> login(@Validated @RequestBody LoginBo loginBo) {
        return userService.login(loginBo);
    }

    @PostMapping("/register")
    public CommonResult<?> register(@RequestBody User user) {
        return userService.register(user);
    }

    @RequestMapping("/queryInfo")
    @PreAuthorize("hasAuthority('user/queryInfo')")
    public CommonResult<UserInfoVo> queryInfo() {
        return userService.queryInfoById(SecurityUtil.getUserId());
    }

    @PostMapping("/updateInfo")
    @PreAuthorize("hasAuthority('user/updateInfo')")
    public CommonResult<?> updateInfo(@RequestBody UserInfoVo userInfoVo) {
        userInfoVo.setUserId(SecurityUtil.getUserId());
        return userService.updateInfoById(userInfoVo);
    }

    @PostMapping("/addAddress")
    @PreAuthorize("hasAuthority('user/addAddress')")
    public CommonResult<?> addAddress(@RequestBody Address address) {
        address.setUserId(SecurityUtil.getUserId());
        return userService.addAddress(address);
    }

    @RequestMapping("/queryAddress")
    @PreAuthorize("hasAuthority('user/queryAddress')")
    public CommonResult<List<Address>> queryAddress() {
        return userService.queryAddressByUserId(SecurityUtil.getUserId());
    }

    @RequestMapping("/deleteAddressById")
    @PreAuthorize("hasAuthority('user/deleteAddressById')")
    public CommonResult<?> deleteAddressById(@RequestBody Map<String, String> map) {
        return userService.deleteAddressById(Long.valueOf(map.get("addressId")));
    }

    @RequestMapping("/changeAddressToDefault")
    @PreAuthorize("hasAuthority('user/changeAddressToDefault')")
    public CommonResult<?> changeAddressToDefault(@RequestBody Map<String, Long> map) {
        map.put("userId", SecurityUtil.getUserId());
        return userService.updateAddressToDefaultById(map);
    }

    @PostMapping("/updatePwd")
    @PreAuthorize("hasAuthority('user/updatePwd')")
    public CommonResult<?> updatePwd(@RequestBody PwdBo pwdBo) {
        pwdBo.setUserId(SecurityUtil.getUserId());
        return userService.updatePassword(pwdBo);
    }

    @RequestMapping("/settleMerchant")
    @PreAuthorize("hasAuthority('user/settleMerchant')")
    public CommonResult<?> settleMerchant(@RequestBody LoginBo loginBo) {
        return userService.addMerchant(loginBo);
    }

}
